package com.csu.jpetstore_reconfiguration_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Cart;
import com.csu.jpetstore_reconfiguration_back.domain.Item;
import com.csu.jpetstore_reconfiguration_back.domain.Product;
import com.csu.jpetstore_reconfiguration_back.mapper.ItemMapper;
import com.csu.jpetstore_reconfiguration_back.mapper.ProductMapper;
import com.csu.jpetstore_reconfiguration_back.domain.User;
import com.csu.jpetstore_reconfiguration_back.mapper.UserMapper;
import com.csu.jpetstore_reconfiguration_back.service.CartService;
import com.csu.jpetstore_reconfiguration_back.mapper.CartMapper;
import com.csu.jpetstore_reconfiguration_back.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.csu.jpetstore_reconfiguration_back.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【t_cart】的数据库操作Service实现
* @createDate 2024-04-23 23:05:03
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements CartService{
    @Autowired
    CartMapper cartMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public CommonResponse<List<CartVO>> getCartItems(HttpServletRequest request) {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);

        QueryWrapper<User>userQueryWrapper=new QueryWrapper<>();
        QueryWrapper<Cart>cartQueryWrapper=new QueryWrapper<>();
        assert user!=null;
        cartQueryWrapper.eq("user_id",user.getUserId());
        List<Cart> cartList= cartMapper.selectList(cartQueryWrapper);
        List<CartVO>CartVOList=new ArrayList<>();

        if(cartList.isEmpty())
        {
            return CommonResponse.createForSuccessMessage("购物车为空");
        }

        for(Cart cart:cartList)
        {
            Item item=itemMapper.selectById(cart.getItemId());
            Product product=productMapper.selectById(item.getProductId());

            userQueryWrapper.eq("user_id",item.getSupplier());
            User supplier_user=userMapper.selectOne(userQueryWrapper);
            CartVO cartVO= cartToCartVO(cart,item,product);
            cartVO.setUserName(user.getUsername());
            cartVO.setSupplierName(supplier_user.getUsername());
            CartVOList.add(cartVO);
        }

        return CommonResponse.createForSuccess(CartVOList);
    }

    @Override
    public CommonResponse<List<CartVO>> getCartItemsByUserId(HttpServletRequest request) {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        assert user!=null;
        QueryWrapper<Cart> cartQueryWrapper=new QueryWrapper<>();
        cartQueryWrapper.eq("user_id",user.getUserId());
        List<Cart> cartList= cartMapper.selectList(cartQueryWrapper);

        QueryWrapper<User>userQueryWrapper=new QueryWrapper<>();
        List<CartVO>CartVOList=new ArrayList<>();

        if(cartList.isEmpty())
        {
            return CommonResponse.createForSuccessMessage(user.getUsername()+"的购物车为空");
        }
        for(Cart cart:cartList)
        {
            Item item=itemMapper.selectById(cart.getItemId());
            Product product=productMapper.selectById(item.getProductId());
            userQueryWrapper.eq("user_id",item.getSupplier());
            User supplier_user=userMapper.selectOne(userQueryWrapper);
            CartVO cartVO= cartToCartVO(cart,item,product);
            cartVO.setUserName(user.getUsername());
            cartVO.setSupplierName(supplier_user.getUsername());
            CartVOList.add(cartVO);
        }

        return CommonResponse.createForSuccess(CartVOList);
    }

    @Override
    public CommonResponse<CartVO> deleteCartItem(String id,HttpServletRequest request) {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        Cart cart=cartMapper.selectById(id);
        Item item=itemMapper.selectById(cart.getItemId());
        Product product=productMapper.selectById(item.getProductId());

        QueryWrapper<User>userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("user_id",item.getSupplier());
        User supplier_user=userMapper.selectOne(userQueryWrapper);
        CartVO cartVO= cartToCartVO(cart,item,product);

        cartVO.setUserName(user.getUsername());
        cartVO.setSupplierName(supplier_user.getUsername());
        QueryWrapper<Cart>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        cartMapper.delete(queryWrapper);

        return CommonResponse.createForSuccess("删除成功",cartVO);
    }
    @Override
    public CommonResponse<CartVO> addCart(Cart cart, HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        cart.setUserId(user.getUserId());
        QueryWrapper<Cart> queryWrapper0=new QueryWrapper<>();
        queryWrapper0.eq("user_id",user.getUserId());
        queryWrapper0.eq("item_id",cart.getItemId());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id",cart.getItemId());
        Item item=itemMapper.selectOne(queryWrapper);
        CartVO cartVO=new CartVO();
        cartVO.setItemId(cart.getItemId());
        cartVO.setUserId(cart.getUserId());
        Cart cart1=cartMapper.selectOne(queryWrapper0);

        QueryWrapper<User>userQueryWrapper=new QueryWrapper<>();
        if(cart1!=null)
        {
            int n=cart.getQuantity()+cart1.getQuantity();
            cart.setQuantity(n);
            cartMapper.updateById(cart);
            Product product=productMapper.selectById(item.getProductId());
            cartVO=cartToCartVO(cart,item,product);
            userQueryWrapper.eq("user_id",item.getSupplier());
            User supplier_user=userMapper.selectOne(userQueryWrapper);
            cartVO.setId(cart1.getId());
            cartVO.setQuantity(n);
            cartVO.setUserName(user.getUsername());
            cartVO.setSupplierName(supplier_user.getUsername());
            return CommonResponse.createForSuccess("添加成功",cartVO);
        }
        cartMapper.insert(cart);
        userQueryWrapper.eq("user_id",item.getSupplier());
        User supplier_user=userMapper.selectOne(userQueryWrapper);
        cartVO.setId(cart.getId());
        cartVO.setQuantity(cart.getQuantity());
        cartVO.setUserName(user.getUsername());
        cartVO.setSupplierName(supplier_user.getUsername());
        return CommonResponse.createForSuccess("添加成功",cartVO);
    }

    @Override
    public CommonResponse<CartVO> updateCart(@PathVariable Cart cart, HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        QueryWrapper<Cart> queryWrapper0=new QueryWrapper<>();
        queryWrapper0.eq("user_id",user.getUserId());
        queryWrapper0.eq("item_id",cart.getItemId());
        Cart cart1=cartMapper.selectOne(queryWrapper0);
        cart1.setQuantity(cart.getQuantity());
        cartMapper.updateById(cart1);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id",cart.getItemId());
        Item item=itemMapper.selectOne(queryWrapper);
        Product product=productMapper.selectById(item.getProductId());
        CartVO cartVO=cartToCartVO(cart,item,product);
        cartVO.setItemId(cart.getItemId());
        cartVO.setUserId(cart.getUserId());
        cartVO.setQuantity(cart.getQuantity());
        cartVO.setId(cart.getId());

        QueryWrapper<User>userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("user_id",item.getSupplier());
        User supplier_user=userMapper.selectOne(userQueryWrapper);
        cartVO.setUserName(user.getUsername());
        cartVO.setSupplierName(supplier_user.getUsername());
        return CommonResponse.createForSuccess("更新成功",cartVO);
    }

    private CartVO cartToCartVO(Cart cart, Item item, Product product){
        CartVO CartVO=new CartVO();
        CartVO.setId(cart.getId());
        CartVO.setItemId(cart.getItemId());
        CartVO.setQuantity(cart.getQuantity());
        CartVO.setUserId(cart.getUserId());

        CartVO.setProductId(item.getProductId());
        CartVO.setCategory(product.getCategory());
        CartVO.setDescn(product.getDescn());
        CartVO.setImgUrl(product.getImgUrl());
        CartVO.setName(product.getName());

        BigDecimal listPrice=new BigDecimal(cart.getQuantity());
        BigDecimal totalprice=listPrice.multiply(item.getListPrice());
        CartVO.setTotalPrice(totalprice);
        return CartVO;
    }
}




